﻿using HY.Common;
using HY.Entity;
using HY.Service;
using HY.Web.Controllers;
using HY.Web.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace HY.Web.Areas.Monitor.Controllers
{
    public class EquipmentBindController : BaseController
    {
        BaseService service = new BaseService();
        // GET: Monitor/EquipmentBind
        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 获取分页列表
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult Index(int page, int limit)
        {
            ReturnModel returnModel = new ReturnModel();
            try
            {
                var code = Request.Form["EquipmentCode"];
                var where = $"E.IsDeleted=0";
                if (!string.IsNullOrWhiteSpace(code))
                    where += $" AND E.EquipmentCode like '%{code}%'";
                var tuple = service.GetPageList<V_EquipmentBindList>("EquipmentBind EB LEFT JOIN Equipment E ON EB.EquipmentID=E.ID LEFT JOIN EquipmentType ET ON E.EquipmentTypeID=ET.ID LEFT JOIN Park P ON EB.ParkID=P.ID  LEFT JOIN Storehouse S ON EB.StorehouseID=S.ID  LEFT JOIN Location L ON EB.LocationID=L.ID", "E.*,ET.TypeName,P.Code ParkCode,S.Code StorehouseCode,L.Code LocationCode", where, page, limit, "E.CreateDate desc");
                returnModel = base.GetReturnModel(tuple.Item1, tuple.Item2);
            }
            catch (Exception ex)
            {
                returnModel = base.GetReturnModel(ex);
                LogHelper.Error("查询设备绑定列表", ex);
            }
            return Json(returnModel);
        }

        public ActionResult Add()
        {
            return View();
        }

        [HttpPost]
        public JsonResult Add(EquipmentBind model)
        {
            ReturnModel returnModel = new ReturnModel();
            LogInfo logInfo = new LogInfo() { UserID = base.UserInfo.UserID, LogType = LogType.Insert };
            try
            {
                model.CreateUserID = base.UserInfo.UserID;
                model.CreateDate = DateTime.Now;
                returnModel.count = service.Add(model);
                returnModel.msg = "保存成功";
                logInfo.LogMessage = $"设备绑定{returnModel.msg}";
                LogHelper.Info(logInfo);
            }
            catch (Exception ex)
            {
                returnModel = base.GetReturnModel(ex);
                returnModel.msg = "保存失败";
                logInfo.LogMessage = $"设备绑定{returnModel.msg}";
                LogHelper.Error(logInfo, ex);
            }
            return Json(returnModel);
        }
    }
}